﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PruebaADONET.DataTypes;
using PruebaADONET.AccesoDatos;

namespace PruebaADONET {
    public partial class FormPruebaADONET : Form {

        //--------------------------------------------------------------------------------------

        public FormPruebaADONET() {
            InitializeComponent();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefresh_Click(object sender, EventArgs e) {
            AccesoDatosSqlClient ad = new AccesoDatosSqlClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshMySQL_Click(object sender, EventArgs e) {
            AccesoDatosMySQLClient ad = new AccesoDatosMySQLClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshPostgress_Click(object sender, EventArgs e) {
            AccesoDatosPostgresClient ad = new AccesoDatosPostgresClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshOLEDBSQLServer_Click(object sender, EventArgs e) {
            AccesoDatosOLEDBClient ad = new AccesoDatosOLEDBClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshOLEDBMySQL_Click(object sender, EventArgs e) {
            AccesoDatosOLEDBMySQLClient ad = new AccesoDatosOLEDBMySQLClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshODBCSQLServer_Click(object sender, EventArgs e) {
            AccesoDatosODBCClient ad = new AccesoDatosODBCClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void btnRefreshDesconectadoMySQL_Click(object sender, EventArgs e) {
            AccesoDatosMySQLClient ad = new AccesoDatosMySQLClient();
            bindingSourcePersonas.DataSource = ad.getAllPersonasDesconectado();
        }

        //--------------------------------------------------------------------------------------

        private void dataGridViewPersonas_SelectionChanged(object sender, EventArgs e) {
            Persona persona = (Persona)(bindingSourcePersonas.Current);
            txtNombre.Text = persona.Nombre;
            txtApellido.Text = persona.Apellido;
            webBrowserPersona.Url = new Uri(persona.URLSitioPreferido);
        }

        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------

        private void btnRefreshEnSegundoPlano_Click(object sender, EventArgs e) {
            progressBarPruebaADONET.Visible = true;
            backgroundWorkerRefresh.RunWorkerAsync();
        }

        //--------------------------------------------------------------------------------------

        private void backgroundWorkerRefresh_DoWork(object sender, DoWorkEventArgs e) {
            AccesoDatosSqlClient ad = new AccesoDatosSqlClient();
            e.Result = ad.getAllPersonas();
        }

        //--------------------------------------------------------------------------------------

        private void backgroundWorkerRefresh_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            if (e.Error == null) {
                bindingSourcePersonas.DataSource = e.Result;
            } else {
                MessageBox.Show((string)e.Result, "Error");
            }
            progressBarPruebaADONET.Visible = false;

        }

        //--------------------------------------------------------------------------------------
    }
}
